/**
 * Created by WLSQ10 on 2016/5/19.
 * 代理商管理
 */
//代理商树形列表信息查询方法
$(function(){
    showAgentTree();
});

//绑定二级代理方法
function showAgentTree() {
    var param = JSON.stringify({"token": localStorage.token});
    var agrntList = "";
    $.fn.serviceInvoke(serviceAddress.Address, 'agent_app_AgentTree', 'showAgentTree.action', param, function (result) {
        debugger;
        if (result.msg == "success") {
            if (result.entity == 3) {
                if (result.list.length == 0) {
                    $(".cd-accordion-menu").html("<div class='text-center' style='margin-top: 20px;'>没有下级代理</div>");
                } else {
                    for (var i = 0; i < result.list.length; i++) {
                        if (result.list[i].sumPay == "" || result.list[i].sumPay == null) {
                            agrntList += '<li class="has-children"><input type="checkbox"  name ="sub-group-' + i + '" id="sub-group-' + i + '"><label for="sub-group-' + i + '")"><p><span style="float: left" class="leftName">' + result.list[i].rightName + '</span> </p><p style="clear: both;">' + result.list[i].leftName + '<button data-id="' + result.list[i].Id + '" data-type="' + result.list[i].type + '" class="btn" style="float: right;border: 0;color:#ff6600;"><span style="margin-left: 10px;" class="rightName" data-name="' + result.list[i].rightName + '">查看详情</span></button></p></label><ul  class="user' + result.list[i].Id + '"></ul></li>';
                        } else {
                            agrntList += '<li class="has-children"><input type="checkbox"  name ="sub-group-' + i + '" id="sub-group-' + i + '"><label for="sub-group-' + i + '")"><p><span style="float: left" class="leftName">' + result.list[i].rightName + '</span></p><p style="clear: both;">' + result.list[i].leftName + '<button data-id="' + result.list[i].Id + '" data-type="' + result.list[i].type + '" class="btn" style="float: right;border: 0;color:#ff6600;"><span style="margin-left: 10px;" class="rightName" data-name="' + result.list[i].rightName + '">查看详情</span></button></p></label><ul  class="user' + result.list[i].Id + '"></ul></li>';
                        }
                    }
                    $(".cd-accordion-menu").html(agrntList);
                }
            }else if(result.entity == 2){
                if (result.list.length == 0) {
                    $(".cd-accordion-menu").html("<div class='text-center' style='margin-top: 20px;'>没有下级代理</div>");
                } else {
                    for (var i = 0; i < result.list.length; i++) {
                        if (result.list[i].sumPay == "" || result.list[i].sumPay == null) {
                            agrntList += '<li class="has-children"><input type="checkbox"  name ="sub-group-' + i + '" id="sub-group-' + i + '"><label for="sub-group-' + i + '")"><p><span style="float: left" class="leftName">' + result.list[i].rightName + '</span><span style="float: right">本月佣金0.00元</span> </p><p style="clear: both;">' + result.list[i].leftName + '<span class="badge" style="margin-left: 15px;background-color: #ff6600">' + result.list[i].sum + '</span><button data-id="' + result.list[i].Id + '" data-type="' + result.list[i].type + '" class="btn" style="float: right;border: 0;color:#ff6600;"><span style="margin-left: 10px;" class="rightName" data-name="' + result.list[i].rightName + '">查看详情</span></button></p></label><ul  class="user' + result.list[i].Id + '"></ul></li>';
                        } else {
                            agrntList += '<li class="has-children"><input type="checkbox"  name ="sub-group-' + i + '" id="sub-group-' + i + '"><label for="sub-group-' + i + '")"><p><span style="float: left" class="leftName">' + result.list[i].rightName + '</span><span style="float: right">本月佣金' + result.list[i].sumPay + '元</span></p><p style="clear: both;">' + result.list[i].leftName + '<span class="badge" style="margin-left: 15px;background-color: #ff6600">' + result.list[i].sum + '</span><button data-id="' + result.list[i].Id + '" data-type="' + result.list[i].type + '" class="btn" style="float: right;border: 0;color:#ff6600;"><span style="margin-left: 10px;" class="rightName" data-name="' + result.list[i].rightName + '">查看详情</span></button></p></label><ul  class="user' + result.list[i].Id + '"></ul></li>';
                        }
                    }
                    $(".cd-accordion-menu").html(agrntList);
                }
            }
            else {
                if (result.list.length == 0) {
                    $(".cd-accordion-menu").html("<div class='text-center' style='margin-top: 20px;'>没有下级代理</div>");
                } else {
                    for (var i = 0; i < result.list.length; i++) {
                        if (result.list[i].sumPay == "" || result.list[i].sumPay == null) {
                            agrntList += '<li class="has-children"><input type="checkbox" name ="group-' + i + '" id="group-' + i + '"><label for="group-' + i + '"  onclick="next2(' + result.list[i].Id + ',' + result.list[i].type + ')"><p><span style="float: left" class="leftName">' + result.list[i].rightName + '</span> <span style="float: right">本月佣金0.00元</span></p><p style="clear: both;">' + result.list[i].leftName + '<span class="badge" style="margin-left: 15px;background-color: #ff6600">' + result.list[i].sum + '</span><button data-id="' + result.list[i].Id + '" data-type="' + result.list[i].type + '" class="btn" style="float: right;border: 0;color:#ff6600;"><span style="margin-left: 10px;" class="rightName" data-name="' + result.list[i].rightName + '">查看详情</span></button></p></label><ul class="sub-group-' + result.list[i].Id + '"></ul></li>';
                        } else {
                            agrntList += '<li class="has-children"><input type="checkbox" name ="group-' + i + '" id="group-' + i + '"><label for="group-' + i + '"  onclick="next2(' + result.list[i].Id + ',' + result.list[i].type + ')"><p><span style="float: left" class="leftName">' + result.list[i].rightName + '</span> <span style="float: right">本月佣金' + result.list[i].sumPay + '元</span></p><p style="clear: both;">' + result.list[i].leftName + '<span class="badge" style="margin-left: 15px;background-color: #ff6600">' + result.list[i].sum + '</span><button data-id="' + result.list[i].Id + '" data-type="' + result.list[i].type + '" class="btn" style="float: right;border: 0;color:#ff6600;"><span style="margin-left: 10px;" class="rightName" data-name="' + result.list[i].rightName + '">查看详情</span></button></p></label><ul class="sub-group-' + result.list[i].Id + '"></ul></li>';
                        }
                    }
                    $(".cd-accordion-menu").html(agrntList);
                }
            }
        }
        findProDetail();
        dealTextLength();
    });
}

//绑定三级代理方法
function next2(id,type){
    var str = base64encode(id+"");
    var param = JSON.stringify({"token": str});
    var classId = ".sub-group-"+id;
    $.fn.serviceInvoke(serviceAddress.Address, 'agent_app_AgentTree','showAgentTree.action', param, function (result) {
        var agrntList1 ="";
        if(result.list.length==0){
        }else{
            for(var i=0;i<result.list.length;i++){
                if(result.list[i].sumPay==""||result.list[i].sumPay==null){
                    agrntList1+='<li class="has-children"><input type="checkbox"  name ="sub-group-'+i+'" id="sub-group-'+i+'"><label for="sub-group-'+i+'")"><p><span style="float: left" class="leftName">'+result.list[i].rightName+'</span> <span style="float: right">本月佣金0.00元</span></p><p style="clear: both;">'+result.list[i].leftName+'<span class="badge" style="margin-left: 15px;background-color: #ff6600">'+result.list[i].sum+'</span><button data-id="'+result.list[i].Id+'" data-type="'+result.list[i].type+'" class="btn" style="float: right;border: 0;color:#ff6600;"><span style="margin-left: 10px;" class="rightName" data-name="'+result.list[i].rightName+'">查看详情</span></button></p></label><ul  class="user'+result.list[i].Id+'"></ul></li>';
                }else{
                    agrntList1+='<li class="has-children"><input type="checkbox"  name ="sub-group-'+i+'" id="sub-group-'+i+'"><label for="sub-group-'+i+'")"><p><span style="float: left" class="leftName">'+result.list[i].rightName+'</span> <span style="float: right">本月佣金'+result.list[i].sumPay+'元</span></p><p style="clear: both;">'+result.list[i].leftName+'<span class="badge" style="margin-left: 15px;background-color: #ff6600">'+result.list[i].sum+'</span><button data-id="'+result.list[i].Id+'" data-type="'+result.list[i].type+'" class="btn" style="float: right;border: 0;color:#ff6600;"><span style="margin-left: 10px;" class="rightName" data-name="'+result.list[i].rightName+'">查看详情</span></button></p></label><ul  class="user'+result.list[i].Id+'"></ul></li>';
                }
            }
            $(classId).html(agrntList1);
        }
        findProDetail();
        dealTextLength();
    });
}

//绑定用户代理方法
function next3(id,type){
    var str = base64encode(id+"");
    var param = JSON.stringify({"token": str});
    var classId = ".user"+id;
    $.fn.serviceInvoke(serviceAddress.Address, 'agent_app_AgentTree','showAgentTree.action', param, function (result) {
        var agrntList2=""
        if(result.list.length==0){
        }else{
            for(var i=0;i<result.list.length;i++){
                if(result.list[i].sumPay==""||result.list[i].sumPay==null){
                    agrntList2+='<li><label><p><span style="float: left" class="leftName">'+result.list[i].rightName+'</span> <span style="float: right">本月佣金0.00元</span></p><p style="clear: both;">'+result.list[i].leftName+'<button data-id="'+result.list[i].Id+'" data-type="'+result.list[i].type+'" class="btn" style="float: right;border: 0;color:#ff6600;"><span style="margin-left: 10px;" class="rightName" data-name="'+result.list[i].rightName+'">查看详情</span></button></p></label></li>';
                }else{
                    agrntList2+='<li><label><p><span style="float: left" class="leftName">'+result.list[i].rightName+'</span> <span style="float: right">本月佣金'+result.list[i].sumPay+'元</span></p><p style="clear: both;">'+result.list[i].leftName+'<button data-id="'+result.list[i].Id+'" data-type="'+result.list[i].type+'" class="btn" style="float: right;border: 0;color:#ff6600;"><span style="margin-left: 10px;" class="rightName" data-name="'+result.list[i].rightName+'">查看详情</span></button></p></label></li>';
                }

            }
            $(classId).html(agrntList2);
        }
        findProDetail();
        dealTextLength();
    });
}


//查询个人详情资料方法
function findProDetail(){
    $(".btn").bind("click",function(e){
        e.stopPropagation();
        var agenttype = $(this).attr("data-type");
        var agentId = $(this).attr("data-id");
        location.href="UserDetail.html?agenttype="+agenttype+"&agentId="+agentId;
    });

}

//处理用户名长度较长溢出问题
function  dealTextLength(){
    var a="";
    for(var i=0;i<$('.leftName').length;i++){
           a = $($('.leftName')[i]).text();
         if (a.length > 10) {
            s = a.substring(0, 10) + "...";
        }else{
             s =a;
         }
        $($('.leftName')[i]).text(s);
    }
}

